import { request } from '../../request/index.js';
import regeneratorRuntime from '../../lib/runtime/runtime.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsObj: {}
  },
  goodsInfo: {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {goods_id} = options
    this.getGoodsDetail(goods_id)
  },
  async getGoodsDetail(goods_id) {
    const result = await request({url:'/goods/detail',data:{goods_id}})
    this.goodsInfo = result
    this.setData({
      goodsObj: {
        goods_name: result.goods_name,
        goods_price: result.goods_price,
        goods_introduce: result.goods_introduce.replace(/\.webp/g, '.jpg'),
        pics: result.pics
      }
    })
  },
  handlePreview(e) {
    const urls = this.goodsInfo.pics.map(v=>v.pics_mid)
    const current =  e.currentTarget.dataset.url
    wx.previewImage({
      current: current,
      urls: urls,
    });
      
  },
  handleCart() {
    let cart = wx.getStorageSync("cart") || [];
    let index = cart.findIndex(v=>v.goods_id === this.goodsInfo.goods_id)
    if(index === -1) {
      this.goodsInfo.num = 1
      this.goodsInfo.checked = true
      cart.push(this.goodsInfo)
    } else {
      cart[index].num++
    }
    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      mask: true
    });
  }

  
})